"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.array2Tree = array2Tree;
exports.treeGetByID = treeGetByID;
function array2Tree({ arr, idKey = 'id', parentIdKey = 'parent_id', groupName = 'children' }) {
    if (!Array.isArray(arr) || !arr.length)
        return;
    let map = {};
    arr.forEach(item => map[item[idKey]] = item);
    let roots = [];
    arr.forEach(item => {
        const parent = map[item[parentIdKey]];
        if (parent) {
            (parent[groupName] || (parent[groupName] = [])).push(item);
        }
        else {
            roots.push(item);
        }
    });
    return roots;
}
function treeGetByID({ Tree, id, idKey = 'id', parentIdKey = 'parent_id', groupName = 'children' }) {
    let o = {};
    Tree.forEach(function (item) {
        if (item.id === id) {
            o = item;
        }
        else if (typeof item[groupName] === 'string' && item[groupName].length > 0) {
            o = treeGetByID({ Tree: item[groupName], id, idKey, parentIdKey, groupName });
        }
    });
    return o;
}
